from django.shortcuts import render_to_response
from django.template import RequestContext
from models import Outcrop, Outcrop_Collection, Image, Excursion
from django.db.models import Q
from haystack.views import basic_search
from userarea.forms import SearchBar
from auth.forms import loginForm
from django.contrib.auth.decorators import login_required
"""
View to load the home page of the website.
""" 
def index(request):
    searchForm = SearchBar()
    form = loginForm()
    outcrop_list = Outcrop.objects.all()
    return render_to_response('main/index.html', {'title':'Home', 'searchForm':searchForm, 'loginForm':form, 'outcrop_list':outcrop_list, 'onLoad':'init()'},context_instance=RequestContext(request))

"""
View to load the outcrop page for a specific outcrop.
""" 
def view_outcrop(request, out_id):
    outcrop = Outcrop.objects.get(pk=out_id)
    excursion_list = Outcrop_Collection.objects.filter(outcrop=outcrop, public=True)
    image_list = Image.objects.filter(outcrop=outcrop, public=True)[:10]
    fromExcursion = False
    searchForm = SearchBar()
    isAdmin = False
    if (request.user is not None) and (not request.user.is_anonymous()):
        try:
            Outcrop.objects.get(pk=out_id, owner=request.user)
            isAdmin = True
        except Outcrop.DoesNotExist:
            isAdmin = False
    
    return render_to_response('main/outcrop.html', {'fromExcursion':fromExcursion, 'outcrop':outcrop,
            'excursion_list':excursion_list, 'searchForm':searchForm, 'image_list':image_list,  'isAdmin':isAdmin, 'onLoad':'init()' } ,context_instance=RequestContext(request))

"""
View to load the outcrop page from an excursion.
@precondition: user must be authenticated.
""" 
@login_required(login_url='/login/')
def view_outcrop_excursion(request, out_id, ex_id):
    outcrop = Outcrop.objects.get(pk=out_id)
    excursion_list = Outcrop_Collection.objects.filter(outcrop=outcrop, public=True)
    excursionItem = Excursion.objects.get(pk=ex_id)
    my_image_list = Image.objects.filter(excursion=excursionItem, outcrop=outcrop)
    #exclude your excursion from the general image list in order to avoid repetition
    image_list = Image.objects.filter(~Q(excursion=excursionItem), outcrop=outcrop, public=True)[:10]
    fromExcursion = True
    searchForm = SearchBar()
    if Outcrop.objects.filter(pk=out_id, owner=request.user).exists():
        isAdmin = True
    else:
        isAdmin = False
    return render_to_response('main/outcrop.html', {'fromExcursion':fromExcursion, 'outcrop':outcrop, 'excursion_list':excursion_list,
            'image_list':image_list, 'searchForm':searchForm, 'my_image_list':my_image_list,  'isAdmin':isAdmin, 'excursionItem':excursionItem, 'onLoad':'init()' } ,context_instance=RequestContext(request))

def search(request):
    searchForm = SearchBar()
    return basic_search(request, extra_context = {'title':'Search', 'hideMenuSearchBar':True, 'searchForm':searchForm}, template='search/search.html')